{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---------\n",
    "\n",
    "* 本学习记录人：许智超    \n",
    "* 学习时间：2021-11-11   \n",
    "* week06   \n",
    "* [参考的文档](https://python.org/doc)\n",
    "* [许老师博客](https://cnblogs.com/xuzhichao)\n",
    "\n",
    "---------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "\n",
    "## 1. 认识[列表](https://docs.python.org/3/tutorial/introduction.html#lists)\n",
    "\n",
    "\n",
    "\n",
    "## 2. 列表的取值\n",
    "\n",
    "## 3. 列表的一般方法\n",
    "\n",
    "## 4. 列表的高级方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 初始列表\n",
    "\n",
    "> 1. 列表的样式： [元素1,元素2,元素3,...]\n",
    "> 2. 列表的性质:\n",
    ">> 1. 列表是有顺序的（Sequence Types 序列类型的数据），第一个元素的位置/索引（index）是 0\n",
    ">> 2. 列表可以存放不同的类型数据， **但是** 一般存放相同类型数据\n",
    ">> 3. 列表可以按照序列数据 进行 取值（index） & 切片（slices）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "username_database = \"Alex;Jack;zhichao;Marry\"\n",
    "username_database_list = [\"Alex\",\"Jack\",\"zhichao\",\"Marry\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack\n",
      "Jack\n"
     ]
    }
   ],
   "source": [
    "# 目标取出每一个账号名称\n",
    "print(username_database[5:9]) #字符串就显得比较麻烦\n",
    "print(username_database_list[1]) # 列表可以按照独立元素进行取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表中可以包含不同的 types （int\\float\\str..也可是列表）数据 * 不常这样用\n",
    "different_types_list = [\"Alex\",18,175.5,[\"广东省广州市广州南方学院\",510970]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "175.5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['广东省广州市广州南方学院', 510970]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "510970"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考： 取值是否需要知道列表的长度？如果列表只有5个元素，\n",
    "# 取值不能取5以上\n",
    "squares = [1, 4, 9, 16, 25]\n",
    "print(squares[0])\n",
    "#print(squares[10]) # IndexError（索引错误）: list index out of range\n",
    "# 一般 列表的取值需要先知道/清楚列表的长度，取值不要超过其长度范围\n",
    "len(squares) # 取值 不应大于 长度5 位置4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 切片 （slices）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares=[1, 4, 9, 16, 25]\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （从左往右取） 9和16：\n",
    "squares[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （从右往左取）9和16\n",
    "squares[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16, 25]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （从左往右取） 9，16，25：结论：取到末尾或者首位都一律不写最好\n",
    "squares[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16, 25]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （从右往左取） 9，16，25：\n",
    "squares[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取1,9？,回顾切片的步长step  [start,stop,step]\n",
    "squares[:3:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 列表取值实践\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u']   # vowels 元音  [\"数字媒体\"]\n",
    "word = \"Milliways\"               # word ：目标文本    文本 1000000个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 请检查 word 中出现了哪些 vowels 中的元素，并打印出来\n",
    "# 知识点：\n",
    "# 1. for 循环和 if条件判断\n",
    "# 2. in 判断是否存在在列表中 \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "# 请检查 vowels 中出现的 word ?\n",
    "for i in vowels:\n",
    "#     print(i)\n",
    "    if i in word:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要查询的文本：fghediqgleijqwpgjdfgqeiojdsk\n",
      "3 e\n",
      "5 i\n",
      "9 e\n",
      "10 i\n",
      "21 e\n",
      "22 i\n",
      "23 o\n"
     ]
    }
   ],
   "source": [
    "# 扩展案例： 枚举：确定其出现的 位置\n",
    "user_word = input(\"请输入你要查询的文本：\")\n",
    "for i,v in enumerate(user_word):\n",
    "    if v in vowels:\n",
    "        print(i,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 列表的一般方法\n",
    "\n",
    "### 本周作业： 课堂堂练习 + 如下表所示的表格 ，总结 列表的所有方法\n",
    "\n",
    "\n",
    "| 列表的方法      | Description | 一般用在什么场景下 |\n",
    "| ----------- | ----------- |----------- |\n",
    "| append      | 列表的新增：增加元素至列表的末尾       | 新建列表 |\n",
    "| clear   |  列表的清除       | 清空列表元素 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 1. append() 列表的新增，增加元素至列表的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack', 'Alex']"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 2. remove() 选择列表的内容/值（values），删除\n",
    ">> * 注意：remove()的一般步骤： 1. 先查询 in 2. 再删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_list = [1,4,4,6,5,4,2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_list.remove(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 6, 5, 2]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove()的一般步骤\n",
    "if \"Mary\" in names: # 先查询，再删除\n",
    "    names.remove(\"Mary\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Jan', 'Jack', 'Alex']"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 3. pop() pop(index=-1, /) 默认的删除 最后一位的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop() # 默认删除最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jan'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Jack']"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除实践：\n",
    "stu_id_分数 = [78,88,77,78,90,90,78,88,90,89,95]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove 时 ，可能需要重复运行 才可以按照指定的要求删除所有的值， \n",
    "# 分数在 低于85分 的删除\n",
    "stu_id_分数 = [78,88,77,78,90,90,78,88,90,89,95]\n",
    "for j in stu_id_分数:\n",
    "    if j<85: #判断是否还有值小于85 \n",
    "        for i in stu_id_分数:\n",
    "            if i < 85:\n",
    "                stu_id_分数.remove(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 90, 90, 88, 90, 89, 95]"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除实践: 需要执行两次 即可删干净\n",
    "stu_id_分数 = [78,88,77,78,90,90,78,88,90,89,95,77,78,98,77,82]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前的列表： [78, 88, 77, 78, 90, 90, 78, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 0 78\n",
      "当值小于85时 要删除的值： 78\n",
      "当前的列表： [88, 77, 78, 90, 90, 78, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 1 77\n",
      "当值小于85时 要删除的值： 77\n",
      "当前的列表： [88, 78, 90, 90, 78, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 2 90\n",
      "当前的列表： [88, 78, 90, 90, 78, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 3 90\n",
      "当前的列表： [88, 78, 90, 90, 78, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 4 78\n",
      "当值小于85时 要删除的值： 78\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 5 90\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 6 89\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 7 95\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 77, 78, 98, 77, 82]\n",
      "循环遍历结果： 8 77\n",
      "当值小于85时 要删除的值： 77\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 78, 98, 77, 82]\n",
      "循环遍历结果： 9 98\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 78, 98, 77, 82]\n",
      "循环遍历结果： 10 77\n",
      "当值小于85时 要删除的值： 77\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 0 88\n",
      "当前的列表： [88, 78, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 1 78\n",
      "当值小于85时 要删除的值： 78\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 2 90\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 3 88\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 4 90\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 5 89\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 6 95\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 78, 98, 82]\n",
      "循环遍历结果： 7 78\n",
      "当值小于85时 要删除的值： 78\n",
      "当前的列表： [88, 90, 90, 88, 90, 89, 95, 98, 82]\n",
      "循环遍历结果： 8 82\n",
      "当值小于85时 要删除的值： 82\n"
     ]
    }
   ],
   "source": [
    "for j in stu_id_分数:\n",
    "    if j<85:\n",
    "        for i,v in enumerate(stu_id_分数):\n",
    "            print(\"当前的列表：\",stu_id_分数)\n",
    "            print(\"循环遍历结果：\",i,v)\n",
    "            if v < 85:\n",
    "                print(\"当值小于85时 要删除的值：\",stu_id_分数.pop(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 90, 90, 88, 90, 89, 95, 98]"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
